<?php

/**
 * User
 * @property	int		$id
 * @property	string	$name
 * @property	string	$password
 * @property	int		$type
 * @property	int		$internalId
 * @property	string	$created
 * @property	string	$updated
 */
class User extends Node {
	const TYPE_STUDENT = 1;
	const TYPE_TEACHER = 2;
	const TYPE_ADMINISTRATOR = 3;
	public static function option() {
		return array(
			'table' => 'User',
			'key' => 'id',
			'columns' => array(
				'id' => 'id',
				'name' => 'username',
				'password' => 'password',
				'type' => 'type',
				'internalId' => 'internal_id',
				'created' => 'created',
				'updated' => 'updated',
				'attributeData' => 'attributes',
			));
	}
	public function generatePassword($pass) {
		$str = $pass . $this->id;
		$hash1 = md5($str);
		$hash2 = md5(substr($hash1, 0, 16)) . $this->id . md5(substr($hash1, 16));
		$hash3 = md5($this->id . $hash2);
		return md5(substr($hash3, 0, 16)) . md5(substr($hash3, 16));
	}

	/**
	 * @return Student
	 */
	public function student() {
		return Student::load($this->internalId);
	}

	/**
	 * @return Teacher
	 */
	public function teacher() {
		return Teacher::load($this->internalId);
	}
}